<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/22
 * Time: 14:11
 */

namespace common\sources\write\taskorder;

use Yii;
use common\models\craftsman\TaskOrder;

class TaskOrderAccess implements ITaskOrderAccess
{
    public function deliverGoods($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,update_time = :update_time,merchant_time = :merchant_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':update_time' => time(),
                ':merchant_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //返款操作
    public function rejectRefund($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET serial_num = :serial_num,bus_pay_url = :bus_pay_url,refund_amount = :refund_amount,order_status = :order_status,actual_price = :actual_price,actual_refunds = :actual_refunds,advance_amount = :advance_amount,
        plat_return_time = :plat_return_time,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':serial_num' => $data['serial_num'],
                ':bus_pay_url' => $data['bus_pay_url'],
                ":refund_amount"=> $data['refund_amount'],
                ':order_status' => $data['order_status'],
                ':actual_price' => $data['refund_amount'],
                ':actual_refunds' => $data['refund_amount'],
                ':advance_amount' => $data['refund_amount'],
                ":plat_return_time" => time(),
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    public function rejectBusRefund($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET refund_amount = :refund_amount,serial_num = :serial_num,bus_pay_url = :bus_pay_url,order_status = :order_status,plat_return_time = :plat_return_time,actual_price = :actual_price,
        actual_refunds = :actual_refunds,advance_amount = :advance_amount,update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':refund_amount'=>$data['refund_amount'],
                ':serial_num'=>$data['serial_num'],
                ':bus_pay_url'=>$data['bus_pay_url'],
                ':order_status'=>$data['order_status'],
                ":plat_return_time" => time(),
                ':actual_price' => $data['refund_amount'],
                ':actual_refunds' => $data['refund_amount'],
                ':advance_amount' => $data['refund_amount'],
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
    //商家发货批量操作
    public function batchTaskOrder($data)
    {
        return TaskOrder::updateAll(["order_status"=>5,"merchant_time"=>time(),"update_time"=>time()],["id"=>$data]);
    }
    //商家批量返款
    public function batchRefund($data)
    {
        return TaskOrder::updateAll(["order_status"=>7,"plat_return_time"=>time(),"update_time"=>time()],["id"=>$data]);
    }
    //商家驳回操作
    public function busReject($data)
    {
        $sql = 'UPDATE ' . TaskOrder::tableName() . ' SET order_status = :order_status,refund_amount = :refund_amount,plat_return_time = :plat_return_time,actual_price = :actual_price,
        actual_refunds = :actual_refunds,advance_amount = :advance_amount,
        update_time = :update_time WHERE id = :id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':order_status' => $data['order_status'],
                ':refund_amount' => $data['refund_amount'],
                ":plat_return_time" => time(),
                ':actual_price' => $data['refund_amount'],
                ':actual_refunds' => $data['refund_amount'],
                ':advance_amount' => $data['refund_amount'],
                ':update_time' => time(),
                ':id' => $data['id']
            ])->execute();
    }
}